package com.gexin.controller.system.plug;

import com.gexin.common.utils.StringUtil;
import com.gexin.core.web.SystemBaseController;
import com.gexin.model.plug.FileTag;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Db;

/**
 * 文件素材管理
 * 
 * @author 肖德明 2016-06-28
 *
 */
@ControllerBind(controllerKey = "/system/plug/file", viewPath = "WEB-INF/system/admin/")
public class FileManageController extends SystemBaseController {
	// private static Log log = Log.getLog(ShopFileManageController.class);
	public void index() {
		render("file_manage.html");
	}

	/** 移动项目标签 /shangjia/file/move_tag?tag_id=1&img_id=1,1,1,2,3 **/
	public void move_tag() {
		Integer tag_id = getParaToInt("tag_id");
		String img_id = getPara("img_id");
		Db.update(String.format("update  t_file set tag_id=%s where id in(%s)",tag_id, img_id));
		success();
	}

	/** 更新分组信息 /shangjia/file/update_tag/id?tag_name= */
	public void update_tag() {
		FileTag tag = FileTag.dao.findById(getParaToInt());
		if (tag == null) {
			fail("文件不存在！");
			return;
		}
		if (!tag.getResId().equals(getResIdByCookie())) {
			fail("非法访问！");
			return;
		}
		String tag_name = getPara("tag_name");
		tag.setName(tag_name);
		if (tag.update()) {
			success(tag);
		} else {
			fail("请稍后再试！");
		}
	}

	

	/** 新增标签 /shangjia/file/add_tag?tag_name=test **/
	public void add_tag() {
		String tag_name = getPara("tag_name");
		if (Db.queryLong(
				"select count(1) from  t_file_tag  where tag_name=? and res_id=?",
				tag_name, getResIdByCookie()) > 0) {
			fail("标签名称已经存在！");
			return;
		}
		if (StringUtil.isNotBlank(tag_name)) {
			FileTag tag = new FileTag();
			tag.setResId(getResIdByCookie());
			tag.setName(tag_name);
			if (tag.save()) {
				success(tag);
			} else {
				fail();
			}
		} else {
			fail("请输入标签名称！");
		}
	}

	/** 删除文件 /shangjia/file/del_file?file_id=1,1,2 **/
	public void del_file() {
		Db.update("update t_file set tag_id=1  where id in("
				+ getPara("file_id") + ")  and res_id=?", getResIdByCookie());
		int count = Db.update("DELETE from t_file  where id in("
				+ getPara("file_id") + ")  and res_id=?", getResIdByCookie());
		if (count > 0) {
			success(count);
		} else {
			fail("请稍后再试！");
		}
	}

}
